
--[[
--内存泄露问题排查
https://www.cnblogs.com/yaukey/p/unity_lua_memory_leak_trace.html
--]]

local snapshot = require("snapshot")
require("timer")

local Test = class("Test")


function Test:ctor()
    self.file_path = "/tmp"
    self.tb_data = {}
    self.memory_num = 0
    self.memory_min = 0
    self.memory_max = 0
end


function Test:onStart( ... )
    self:snapshotStart()
    self:test1()
    --self:snapshotEnd()
end

function Test:snapshotStart()
    self.memory_num = collectgarbage("count")
    self.memory_min = self.memory_num
    self.memory_max = self.memory_num
    collectgarbage("collect")
    snapshot.m_cConfig.m_bAllMemoryRefFileAddTime = false
    snapshot.m_cConfig.m_bComparedMemoryRefFileAddTime = false
    snapshot.m_cMethods.DumpMemorySnapshot(self.file_path, "1-Before", -1, "_G", _G)
end

function Test:snapshotEnd()
    -- Dump memory snapshot again.
    local memory = collectgarbage("count")
    if self.memory_min > memory then
        self.memory_min = memory
    end
    if  self.memory_max < memory then
        self.memory_max = memory
    end
    g.log:info("memory cur: ", memory, self.memory_min)
    collectgarbage("collect")
    g.log:info("memory add: ", memory - self.memory_num)
    g.log:info("memory cur: ", memory)
    g.log:info("memory min: ", self.memory_min)
    g.log:info("memory max: ", self.memory_max)
    snapshot.m_cMethods.DumpMemorySnapshot(self.file_path, "2-After", -1, "_G", _G)
    -- 比较两份内存快照结果文件，打印文件 strResultFilePathAfter 相对于 strResultFilePathBefore 中新增的内容。
    -- strSavePath - 快照保存路径，不包括文件名。
    -- strExtraFileName - 添加额外的信息到文件名，可以为 "" 或者 nil。
    -- nMaxRescords - 最多打印多少条记录，-1 打印所有记录。
    -- strResultFilePathBefore - 第一个内存快照文件。
    -- strResultFilePathAfter - 第二个用于比较的内存快照文件。
    -- MemoryReferenceInfo.m_cMethods.DumpMemorySnapshotComparedFile(strSavePath, strExtraFileName, nMaxRescords, strResultFilePathBefore, strResultFilePathAfter)
    snapshot.m_cMethods.DumpMemorySnapshotComparedFile(self.file_path, "Compared", -1, self.file_path.."/LuaMemRefInfo-All-[1-Before].txt", self.file_path.."/LuaMemRefInfo-All-[2-After].txt")
end

--
function Test:test1( ... )
    print("start test1")


    local timer = new("timer")
    local info  = {
        sec = 5,
        callback = handler(self, self.timerTestCallback),
        loop = true,
        param = "3333333",
    }
    timer:start(info)
    --g.sleep(800)

end

function Test:timerTestCallback(...)
    --self:snapshotStart()
    g.log:debug("i am timerTestCallback!", ...)
    table.insert(self.tb_data, ""..math.random())
    print("self.tb_data:", tostring(self.tb_data))
    self:snapshotEnd()
end

--任务定时器
function Test:test2( ... )
    print("任务定时器=================")
    local timer = new("timer")
    local myco = nil
    local info  = {
        time = {
            day = 3,
            hour = 13,
            min = 5,
            sec = 0,
        },
        callback = handler(self, self.timerTestCallback),
        loop = false,
        param = "3333333",
        do_next_date = true,
        -- do_next_week 是否下周继续
    }
    local co = timer:startTask(info)
    print("timer left ", timer:leftTime(co))

end


return Test